package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.crypto.SecretKey;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import code.KeyStoreManager;
import code.Language;
import code.MessageAuthenticationCode;
import code.Settings;

public class DataIntegrityList extends JPanel {

	private static final long serialVersionUID = 1L;
	Color generate = new Color(59, 89, 152);
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JLabel jLabel = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	ATOCOmain atocoframe;
	MessageAuthenticationCode mc = new MessageAuthenticationCode();
	private KeyStoreManager ksm;
	private JLabel jLabel3 = null;
	private int count;
	private JLabel jLabel4 = null;
	private HashMap labels;
	private Settings s;
	private JLabel jLabel5 = null;
	private JLabel jLabel6 = null;
	/**
	 * This is the default constructor
	 */
	public DataIntegrityList(ATOCOmain atocoframe) {
		super();
		s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "DataIntegrityList", (String) s.global.get("installPath"));
		this.atocoframe=atocoframe;
		mc.read();
		initialize();
		
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel6 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/delete.gif"));
		jLabel6.setBounds(new Rectangle(20, 403, 25, 20));
		jLabel6.setVisible(false);
		jLabel5 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/accepticon.gif"));
		jLabel5.setBounds(new Rectangle(20, 377, 25, 20));
		jLabel5.setVisible(false);
		jLabel4 = new JLabel();
		jLabel4.setBounds(new Rectangle(41, 323, 23, 16));
		jLabel3 = new JLabel();
		jLabel3.setBounds(new Rectangle(67, 323, 391, 16));
		jLabel2 = new JLabel();
		jLabel2.setBounds(new Rectangle(50, 375, 700, 20));
		jLabel2.setText((String) labels.get("jLabel2"));
		jLabel2.setVisible(false);
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(50, 400, 700, 20));
		jLabel1.setText((String) labels.get("jLabel1"));
		jLabel1.setForeground(Color.red);
		jLabel1.setVisible(false);
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(32, 15, 354, 16));
		jLabel.setText((String) labels.get("jLabel"));
		this.setSize(768, 459);
		this.setLayout(null);
		this.setBackground(Color.WHITE);
		this.add(getJScrollPane(), null);
		this.add(jLabel, null);
		this.add(getJButton(), null);
		this.add(getJButton1(), null);
		this.add(jLabel1, null);
		this.add(jLabel2, null);
		this.add(jLabel3, null);
		this.add(jLabel4, null);
		this.add(jLabel5, null);
		this.add(jLabel6, null);
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(20, 76, 550, 220));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {

			Vector<Object> rowData = new Vector<Object>();

			Set set = mc.logmac.keySet();
			Iterator it = set.iterator();
			while(it.hasNext()) {
				String al = (String) it.next();
				
					Vector<Object> row = new Vector<Object>();
					row.add(al);
					StringTokenizer tk= new StringTokenizer((String)mc.logmac.get(al), ",");
					while(tk.hasMoreTokens()){
						
						String s=tk.nextToken();
						
						row.add(s);
						
					}
					
					

					rowData.add(row);
				}

			Vector<String> columnNames = new Vector<String>();
			columnNames.add((String) labels.get("col1"));
			columnNames.add((String) labels.get("col2"));
			columnNames.add((String) labels.get("col3"));
			columnNames.add((String) labels.get("col4"));
			columnNames.add((String) labels.get("col5"));
			columnNames.add((String) labels.get("col6"));


			if (!rowData.isEmpty()) {
				DefaultTableModel model = new DefaultTableModel(rowData, columnNames);

				jTable = new JTable(model);
				count = jTable.getModel().getRowCount();
				jLabel3.setText((String) labels.get("jLabel3") + " " + count);
				jLabel3.setFont(new java.awt.Font("Calibri", Font.ITALIC, 14));
				ImageIcon image = new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nexticonsmall.gif");
				jLabel4.setIcon(image);
				JTableHeader baslik = jTable.getTableHeader();
				baslik.setForeground(Color.white);
				baslik.setBackground(generate);
				baslik.setFont(new Font("Arial", Font.BOLD, 13));
				
				 
				


			}

		}
		return jTable;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(600, 76, 133, 46));
			jButton.setText((String) labels.get("jButton"));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 
					browse();
					jButton1.setEnabled(true);
				}
			});
		}
		return jButton;
	}

	public void browse(){
		JFileChooser fc = new JFileChooser(); 
		fc.setDialogTitle("ATOCO");
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fc.setMultiSelectionEnabled(true);
		fc.showOpenDialog(this); 

		try { 
			// Open an input stream 
			File f= fc.getSelectedFile();
			//File[] f =fc.getSelectedFiles();
			System.out.println(f);
			if(!f.equals("null")){
				String s=f.getPath();
				mc.file=s;

			}

		}catch(Exception e){

		}

	}
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(600, 151, 133, 46));
			jButton1.setText((String) labels.get("jButton1"));
			jButton1.setEnabled(false);
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); 

					JLabel label = new JLabel("Please enter your password:");
					JPasswordField jpf = new JPasswordField();
					JOptionPane.showConfirmDialog(null,
					  new Object[]{label, jpf}, "Password:",
					  JOptionPane.OK_CANCEL_OPTION);
					ksm = new KeyStoreManager(
							(String)Settings.global.get("personalKeyStoreFile"), 
							(String)Settings.global.get("personalKeyStorePassword"));
					if (ksm.getKeyStore() == null) {
						System.out.println("Keystore file not found. Creating one.");
						ksm.createKeyStore(
								(String)Settings.global.get("personalKeyStoreFile"), 
								(String)Settings.global.get("personalKeyStorePassword"));
					} 

					if(!jpf.getText().equals("")){
						System.out.println("\n\n\n\n\n\n\n"+jTable.getValueAt(jTable.getSelectedRow(), 1).toString());
						SecretKey key=ksm.getSecretKey(jTable.getValueAt(jTable.getSelectedRow(), 1).toString(), jpf.getText());
						System.out.println(key);
						mc.setKey(key);
						mc.algorithm=jTable.getValueAt(jTable.getSelectedRow(), 3).toString();
						mc.keyalias=jTable.getValueAt(jTable.getSelectedRow(), 1).toString();
						mc.calculateMAC(mc.algorithm, mc.getKey(), mc.keyalias, mc.file);
						if(mc.digestB64.equals(jTable.getValueAt(jTable.getSelectedRow(), 2).toString())){
							jLabel2.setVisible(true);
							jLabel5.setVisible(true);
							jLabel6.setVisible(false);
							jLabel1.setVisible(false);
						}else{
							jLabel2.setVisible(false);
							jLabel6.setVisible(true);
							jLabel5.setVisible(false);
							jLabel1.setVisible(true);
						}
						
					}
				}
			});
		}
		return jButton1;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
